package utils

import "strings"

func RSplitN(str string, sep string, n int) []string {
	if n == 0 {
		return nil
	}
	spCount := strings.Count(str, sep)
	allSplit := strings.SplitN(str, sep, -1)
	if n > spCount || n < 0 {
		return allSplit
	}
	resSplit := []string{Reduce(allSplit[:len(allSplit)-(n-1)], func(i interface{}, i2 interface{}) interface{} {
		return i.(string) + (i2).(string)
	}).(string)}
	for i := len(allSplit) - (n - 1); i < len(allSplit); i++ {
		resSplit = append(resSplit, allSplit[i])
	}
	return resSplit
}
